package com.kadron.dao;

import javax.persistence.NoResultException;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;

import com.kadron.entity.UserImage;

@AutoCreate
@BypassInterceptors
@Name("userImageDAO")
@Scope(ScopeType.EVENT)
public class KUserImageDAO extends KDAO<UserImage> {

	public UserImage find(long userImageId) {
		StringBuffer buffer = new StringBuffer(
				"SELECT u_ FROM UserImage u_ WHERE u_.id = :id");
		javax.persistence.Query query = getEntityManager().createQuery(buffer.toString());
		query.setParameter("id", userImageId);
		try {			
			return (UserImage) query.setMaxResults(1).getSingleResult();
		} catch (NoResultException e) {
			return null;
		}

	}
	
	public UserImage findByUser(long userId) {
		StringBuffer buffer = new StringBuffer(
		"SELECT u_ FROM UserImage u_ WHERE u_.userId = :uid");
		javax.persistence.Query query = getEntityManager().createQuery(buffer.toString());
		query.setParameter("uid", userId);
		try {			
			return (UserImage) query.setMaxResults(1).getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
		
	}	
	
	public String findThumb(long userId) {
		StringBuffer buffer = new StringBuffer(
		"SELECT u_.thumb FROM UserImage u_ WHERE u_.userId = :uid");
		javax.persistence.Query query = getEntityManager().createQuery(buffer.toString());
		query.setParameter("uid", userId);
		try {			
			return (String) query.setMaxResults(1).getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
		
	}	
}